﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.DAL;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.MODEL;
using System.Collections.Generic;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Dispense : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        private DispenseTypeBLL disTypeBLL = new DispenseTypeBLL();
        //private D_Asset assDAL = new D_Asset();
        private AssetBLL assBLL = new AssetBLL();
        private MasterWarehouseBLL whBLL = new MasterWarehouseBLL();
        private HrmJobSiteBLL jsBLL = new HrmJobSiteBLL();
        private DispenseDAL disBLL = new DispenseDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetKeyValue();
                LoadDispenseType();
                LoadAssetCode();
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            disBLL.Insert(txtDispenceName.Text
                , txtDispenseDate.TextWithLiterals
                , cmbAssetCode.SelectedValue
                , cn.ConvertNullInt(cmbWarehouse.SelectedValue)
                , cn.ConvertNullInt(cmbJobSite.SelectedValue)
                , Convert.ToInt32(cmbDispenseType.SelectedValue)
                , txtDestination.Text
                , txtOther.Text
                , Convert.ToInt32(Session["EmpSeq"]));
            RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            RadAjaxManager1.ResponseScripts.Add(@"CloseWindow();");
        }
    #region LoadData
    // Set Default Date-today    
        public void GetKeyValue()
        {
            string day = "";
            string month = "";
            string year = "";

            string[] date_today = DateTime.Now.ToShortDateString().Split('/');
            if (date_today.Length > 0)
            {
                if (date_today[0].Length < 2)
                {
                    day = "0" + date_today[0];
                }
                else
                {
                    day = date_today[0];
                }
                if (date_today[1].Length < 2)
                {
                    month = "0" + date_today[1];
                }
                else
                {
                    month = date_today[1];
                }

                year = date_today[2];
                //txtDate.Text = day + "/" + month + "/" + year;
                txtDispenseDate.Text = day + "/" + month + "/" + year;
            }
        }
        private string SetstrDate()
        {
            string day = "";
            string month = "";
            string year = "";
            string strDate = "";

            string[] date_today = txtDispenseDate.TextWithLiterals.Split('/');
            if (date_today.Length > 0)
            {
                if (date_today[0].Length < 2)
                {
                    day = date_today[0];
                }
                else
                {
                    day = date_today[0];
                }
                if (date_today[1].Length < 2)
                {
                    month = date_today[1];
                }
                else
                {
                    month = date_today[1];
                }
                int iyear = Convert.ToInt32(date_today[2])- 543;
                year = Convert.ToString(iyear);
                //txtDate.Text = day + "/" + month + "/" + year;
                strDate = day + "/" + month + "/" + year;                
            }
            return strDate;
        }

        public void LoadAssetCode()
        {
            //DataSet ListDataCommand = assDAL.GetData(cn.ConvertNullInt(""));
            //DataTable table = ListDataCommand.Tables[0];
            //cmbAssetCode.DataSource = ListDataCommand.Tables[0];
            //cmbAssetCode.DataBind();
        }
        public void LoadDispenseType()
        {
            DataSet ListDataCommand = disTypeBLL.GetDispenseType(cn.ConvertNullInt(""));
            DataTable table = ListDataCommand.Tables[0];
            cmbDispenseType.DataSource = ListDataCommand.Tables[0];
            cmbDispenseType.DataBind();
        }
        protected void cmbAssetCode_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<AssetInfo> infoList = assBLL.GetAsset((int?)null, e.Text);

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbAssetCode.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Sequence.ToString()));

                TableRow tr = new TableRow();

                TableCell tdCode = new TableCell();
                Label lblCode = new Label();
                lblCode.Text = infoList[i].Code.ToString();
                tdCode.Width = Unit.Pixel(150);
                tdCode.Controls.Add(lblCode);
                tr.Cells.Add(tdCode);

                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(250);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);

                ((Table)cmbAssetCode.Items[j].FindControl("tableAssetCode")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbJobSite_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<HrmJobSiteInfo> infoList = jsBLL.GetJobSite(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbJobSite.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
            }

            e.Message = cn.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbWarehouse_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<MasterWarehouseInfo> infoList = whBLL.GetMasterWarehouse(SystemInfo._ActiveRecordStatus);// .GetListFillByName(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbWarehouse.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
            }

            e.Message = cn.GetStatusMessage(endOffset, infoList.Count);
        }
        //protected void rbDispenseFrom_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string strDispenseFrom = rbDispenseFrom.SelectedValue;
        //    if (strDispenseFrom == "1")
        //    {
        //        cmbWarehouse.Visible = true;
        //        cmbJobSite.Visible = false;
        //        txtOther.Visible = false;
        //        cmbJobSite.ClearSelection();
        //        cmbJobSite.Text = "";
        //        txtOther.Text = "";
        //    }
        //    else if (strDispenseFrom == "2")
        //    {
        //        cmbWarehouse.Visible = false;
        //        cmbJobSite.Visible = true;
        //        txtOther.Visible = false;
        //    }
        //    else
        //    {
        //        cmbWarehouse.Visible = false;
        //        cmbJobSite.Visible = false;
        //        txtOther.Visible = true;
        //    }
        //    return;
        //}
        protected void rbWarehouse_CheckedChanged(object sender, EventArgs e)
        {
            cmbWarehouse.Visible = true;
            cmbJobSite.Visible = false;
            txtOther.Visible = false;
            cmbJobSite.ClearSelection();
            cmbJobSite.Text = "";
            txtOther.Text = "";
        }
        protected void rbJobsite_CheckedChanged(object sender, EventArgs e)
        {
            cmbWarehouse.Visible = false;
            cmbJobSite.Visible = true;
            txtOther.Visible = false;
            cmbWarehouse.ClearSelection();
            cmbWarehouse.Text = "";
            txtOther.Text = "";
        }
        protected void rbOther_CheckedChanged(object sender, EventArgs e)
        {
            cmbWarehouse.Visible = false;
            cmbJobSite.Visible = false;
            txtOther.Visible = true;
            cmbWarehouse.ClearSelection();
            cmbWarehouse.Text = "";
            cmbJobSite.ClearSelection();
            cmbJobSite.Text = "";
        }
        
    #endregion
        
    }
}
